import { IOption, AxiosPromise } from '../types/base'
import http from '../http'
import qs from 'qs'

export function GET(options: IOption): AxiosPromise {
	return http.createAxiosInstance({
		...options,
		method: 'GET',
	})
}

export function POST(options: IOption) {
	return http.createAxiosInstance({
		...options,
		method: 'POST',
		headers: { 'Content-Type': 'application/json;charset=UTF-8' },
	})
}

export function POST_QS(options: IOption) {
	return http.createAxiosInstance({
		...options,
		method: 'POST',
		headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
		// 参数序列化
		transformRequest: (data, headers) => {
			return qs.stringify(data)
		},
	})
}

export function UPLOAD(options: IOption) {
	return http.createAxiosInstance({
		...options,
		method: 'POST',
		headers: { 'Content-Type': 'multipart/form-data' },
		// formData 传参
		transformRequest: (data, headers) => {
			const params = new FormData()
			Object.keys(data).forEach((key) => {
				params.append(key, data)
			})
			return qs.stringify(params)
		},
	})
}
